﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS; 
namespace QLVeChuyenBay
{
    public partial class frmVeChuyenBay : Form
    {
        public string tuyenBay;
        public List<BanVeChuyenBayDTO> dsbanVeCB;
        public List<HanhKhachDTO> dsHK;
        private List<ChuyenBayDTO> dsCB;

        public List<ChuyenBayDTO> DsCB
        {
            get { return dsCB; }
            set { dsCB = value; }
        }
        public List<TuyenBayDTO> dsTB;
        public List<HangVeDTO> dsHV;
        
        public frmVeChuyenBay()
        {
            InitializeComponent();
        }

        private void btn_huyBo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
            
        }

        private void frmVeChuyenBay_Load(object sender, EventArgs e)
        {
            dsHV = new List<HangVeDTO>();
            HangVeBUS hvbus = new HangVeBUS();
            dsHV = hvbus.GetAll();
            cbb_hangVe.DataSource = dsHV;
            cbb_hangVe.DisplayMember = "TenHangVe";
            cbb_hangVe.ValueMember = "MaHangVe";
            cbb_hangVe.SelectedIndex = -1;
            cbb_hangVe.Text = "Chọn hạng vé";
            DsCB = new List<ChuyenBayDTO>();
            ChuyenBayBUS cbbus = new ChuyenBayBUS();
            DsCB = cbbus.GetCB();
            cbb_maCB.DataSource = DsCB;
            cbb_maCB.DisplayMember = "MaCB";
            cbb_maCB.ValueMember = "MaCB";
            cbb_maCB.SelectedIndex = -1;
            cbb_maCB.Text = "Chọn mã chuyến bay";
            label15.Text = "";
            loadlaidatagirdview();

        }

        private void loadlaidatagirdview()
        {
            dsbanVeCB = new List<BanVeChuyenBayDTO>();
            BanVeChuyenBayBUS banvecbbus = new BanVeChuyenBayBUS();
            dsbanVeCB = banvecbbus.loadvecb();
            for (int i = 0; i < dsbanVeCB.Count; i++)
            {
                dgv_LoadCB.Rows.Add(i + 1, dsbanVeCB[i].MaChuyenBay, dsbanVeCB[i].MaHanhKhach, dsbanVeCB[i].tenHK, dsbanVeCB[i].tenHangVe);

            }
        }

        private void cbb_hangVe_SelectedIndexChanged(object sender, EventArgs e)
        {

            
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txt_maHK_TextChanged(object sender, EventArgs e)
        {
            string i = txt_maHK.Text;
            HanhKhachDTO dto = new HanhKhachDTO();
            HanhKhachBUS bus = new HanhKhachBUS();
            if (!String.IsNullOrEmpty(i))
            {
                
                dto = bus.layCt(i);
                if (dto.MaHK ==null)
                {
                    label15.Text = "Không có thông tin hành khách này,vui lòng đăng ký";
                   // MessageBox.Show("Không có Khách hàng nào trong CSDL");
                    txt_HoTen.Text = "";
                    txt_CMND.Text = "";
                    txt_dienThoai.Text = "";
                }
                else
                {
                    label15.Text = "";
                    txt_HoTen.Text = dto.TenHK.ToString();
                    txt_CMND.Text = dto.CMND1.ToString();
                    txt_dienThoai.Text = dto.DienThoai.ToString();
                }
            }
            else
            {
                label15.Text = "";
                txt_HoTen.Text = "";
                txt_CMND.Text = "";
                txt_dienThoai.Text = "";
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btn_taoMoi_Click(object sender, EventArgs e)
        {
            txt_HoTen.Text = "";
            txt_maHK.Text = "";
            txt_CMND.Text = "";
            txt_dienThoai.Text = "";
           txt_giaTien.Text = ""; 
           txt_gioBay.Text = "";
           txt_sanBayDen.Text = "";
           txt_sanBayDi.Text = "";
           txt_thoiGianBay.Text = "";
           txt_soGheTrong.Text = "";
           cbb_hangVe.SelectedIndex = -1;
           cbb_hangVe.Text = "Chọn hạng vé";
           cbb_maCB.SelectedIndex = -1;
           cbb_maCB.Text = "Chọn mã chuyến bay";
        }

        private void cbb_hangVe_DropDown(object sender, EventArgs e)
        {

        }

        private void btn_banVe_Click(object sender, EventArgs e)
        {
            if (KiemTraNhapDL() == true)
            {
                if (Int32.Parse(txt_soGheTrong.Text) != 0)
                {
                    BanVeChuyenBayDTO bv = new BanVeChuyenBayDTO();

                    bv.MaChuyenBay = cbb_maCB.SelectedValue.ToString();
                    bv.MaHangVe = cbb_hangVe.SelectedValue.ToString();
                    bv.MaHanhKhach = txt_maHK.Text;
                    bv.GiaTien = float.Parse(txt_giaTien.Text);
                    BanVeChuyenBayBUS bus = new BanVeChuyenBayBUS();
                    int res = bus.BanVeChuyenBay(bv);



                    if (res >= 0)
                    {
                        TinhTrangVeDTO capnhatve = new TinhTrangVeDTO();
                        TinhTrangVeBUS bus1 = new TinhTrangVeBUS();
                        string k = bv.MaHangVe;
                        string i = bv.MaChuyenBay;
                        int res1 = bus1.CapNhatSoGheTrong(capnhatve, i, k);
                        int res2 = bus1.CapNhatSoGheDat(capnhatve, i, k);
                        MessageBox.Show("Bán vé thành công");
                        dgv_LoadCB.Rows.Clear();
                        loadlaidatagirdview();
                    }
                    else
                        MessageBox.Show("Vé chưa được bán");

                }
                else 
                {
                    MessageBox.Show("Hết vé");
                
                }
            }

        }
        public bool KiemTraNhapDL()
        {
            if (txt_sanBayDi.Text == "")
            {
                MessageBox.Show("Chưa chọn chuyến bay");
                return false;
            }
            if (txt_HoTen.Text == "")
            {
                MessageBox.Show("Chưa chọn hành khách ");
                return false;
            }
            if (txt_giaTien.Text == "")
            {
                MessageBox.Show("Chưa chọn hạng vé ");
                return false;
            }
            return true;

        }

        private void txt_giaTien_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbb_maCB_SelectionChangeCommitted(object sender, EventArgs e)
        {
            loadcbboxchuyenbay();
        }

        private void loadcbboxchuyenbay()
        {
            if (cbb_maCB.SelectedIndex != -1 && cbb_maCB.Focused == true)
            {
                string i = cbb_maCB.SelectedValue.ToString();
                cbb_hangVe.SelectedIndex = -1;
                txt_giaTien.Text = "";
                ChuyenBayDTO dto = new ChuyenBayDTO();
                ChuyenBayBUS bus = new ChuyenBayBUS();
                dto = bus.layCt(i);
                tuyenBay = dto.MaTB;
                dtp_ngayBay.Format = DateTimePickerFormat.Custom;
                dtp_ngayBay.CustomFormat = "dd/MM/yyyy";
                dtp_ngayBay.Value = dto.NgayGio;
                txt_thoiGianBay.Text = dto.ThoiGianBay.ToString();
                txt_sanBayDen.Text = dto.sanbayDen.ToString();
                txt_sanBayDi.Text = dto.SanbayDi.ToString();
                txt_gioBay.Text = dto.NgayGio.ToString("hh:mm:ss:tt");

            }
        }

        private void cbb_hangVe_SelectionChangeCommitted(object sender, EventArgs e)
        {

            if (cbb_hangVe.SelectedIndex != -1 && cbb_hangVe.Focused == true && tuyenBay != null)
            {
                string hv = cbb_hangVe.SelectedValue.ToString();
                HangVeDTO dto = new HangVeDTO();
                HangVeBUS bus = new HangVeBUS();
                dto = bus.layCt(tuyenBay, hv);
                TinhTrangVeDTO dtottv = new TinhTrangVeDTO();
                TinhTrangVeBUS busttv = new TinhTrangVeBUS();
                string k = cbb_hangVe.SelectedValue.ToString();
                string m = cbb_maCB.SelectedValue.ToString();
                dtottv = busttv.loadsoghe(m, k);
                txt_soGheTrong.Text = dtottv.SoGheTrong.ToString();
                if (dto.DonGia != null)
                {
                    txt_giaTien.Text = dto.DonGia.ToString();
                    
                }
            
            }
        }

        private void dgv_LoadCB_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btn_khmoi_Click(object sender, EventArgs e)
        {
            frmHanhKhach hk = new frmHanhKhach();
            hk.Show();
        }

        private void cbb_maCB_TextChanged(object sender, EventArgs e)
        {
            loadcbboxchuyenbay();
        }
    }
}
